package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/2/24 0:17
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);

        String urlParam = this.makeUrlParam(searchParam);

        //面包屑处理，propsParamList    trademarkParam
        String trademarkParam = this.makeTrademark(searchParam);

        List<Map<String, String>> propsParamList = this.makeProp(searchParam.getProps());

        Map<String, Object> orderMap = this.makeOrderMap(searchParam.getOrder());

        model.addAttribute("urlParam", urlParam);
        model.addAttribute("searchParam", searchParam);
        model.addAttribute("trademarkParam", trademarkParam);
        model.addAttribute("propsParamList", propsParamList);
        model.addAttribute("orderMap", orderMap);
        model.addAllAttributes(result.getData());
        return "list/index";
    }

    private Map<String, Object> makeOrderMap(String order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
        } else {
            //    给出默认排序规则
            map.put("type", "1");//综合   hotScore
            map.put("sort", "asc");
        }

        return map;
    }

    /**
     * 制作品台属性
     *
     * @param props
     * @return
     */
    private List<Map<String, String>> makeProp(String[] props) {
        List<Map<String, String>> list = new ArrayList<>();
        //判断用户是否根据平台属性值进行过滤数据
        if (props != null && props.length > 0) {
            //    prop是一个数组，此时需要进行遍历
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    HashMap<String, String> propMap = new HashMap<>();
                    propMap.put("attrId", split[0]);
                    propMap.put("attrValue", split[1]);
                    propMap.put("attrName", split[2]);
                    list.add(propMap);
                }
            }
        }
        return list;
    }

    private String makeTrademark(SearchParam searchParam) {
        //判断用户是否点击了品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌:" + split[1];
            }
        }
        return null;
    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuffer urlParam = new StringBuffer();

        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }

        //判断用户是否根据关键字检索
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }

        //判断是否根据品牌检索,这个一般在后面追加，要加上&符号
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            if (urlParam.length() > 0) {
                urlParam.append("&trademark=").append(searchParam.getTrademark());
            }
        }

        //判断用户是否根据平台属性值检索数据
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            //    props是一个数组，此时需要进行循环遍历，拼接数据
            for (String prop : props) {
                if (urlParam.length() > 0) {
                    urlParam.append("&props=").append(prop);
                }
            }
        }

        return "list.html?" + urlParam.toString();
    }

}
